package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceImpl implements UserService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public User login(String username, String password){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession(true);
        //3.获取BrandMapper
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        //4.调用方法
        User user=mapper.select(username,password);
        //5.释放资源
        sqlSession.close();
        return user;
    }
    public boolean register(User user){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession(true);
        //3.获取BrandMapper
        UserMapper mapper=sqlSession.getMapper(UserMapper.class);
        //4.判断用户名是否存在
        User u=mapper.selectByUserName(user.getUserName());
        if(u==null){
            mapper.add(user);
//            sqlSession.commit();

        }else{
            //用户存在

        }
        return u==null?true:false;
    }
}
